package com.yangsir.mysite.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yangsir.mysite.Dto.CommentDto;
import com.yangsir.mysite.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author yf
* @description 针对表【comment(文章的评论)】的数据库操作Mapper
* @createDate 2024-06-22 16:55:16
* @Entity generator.pojo.Comment
*/
public interface CommentMapper extends BaseMapper<Comment> {
    @Select({
            "SELECT c1.*, c2.*",
            "FROM comment c1",
            "LEFT JOIN comment c2 ON c1.id = c2.parent_id"
    })
    @Results({
            @Result(column = "c1.id", property = "parentComment.id"),
            @Result(column = "c1.article_id", property = "parentComment.articleId"),
            @Result(column = "c1.parent_id", property = "parentComment.parentId"),
            @Result(column = "c1.reply_id", property = "parentComment.replyId"),
            @Result(column = "c1.avater_url", property = "parentComment.avatarUrl"),
            @Result(column = "c1.user_id", property = "parentComment.userId"),
            @Result(column = "c1.context", property = "parentComment.context"),
            @Result(column = "c1.is_delete", property = "parentComment.isDelete"),
            @Result(column = "c1.create_time", property = "parentComment.createTime"),
            @Result(column = "c1.update_time", property = "parentComment.updateTime"),
            @Result(column = "c2.id", property = "replies", javaType = List.class, many = @Many(select = "com.yangsir.mysite.mapper.CommentMapper.mapToComment"))
    })
    List<CommentDto> getCommentsWithReplies();

    @Select("SELECT * FROM comment WHERE id = #{id}")
    Comment mapToComment(@Param("id") Long id);
}




